package com.alan.wlmediaplayer.view;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;

import com.alan.wlmediaplayer.R;
import com.alan.wlmediaplayer.databinding.WlMenuViewBinding;
import com.alan.wlmediaplayer.utils.WlSetting;
import com.lib.basex.utils.LToastManager;
import com.lib.basex.widget.baselayout.LRelativeLayout;

import java.util.HashMap;

/**
 * @author Alan
 * 时 间：11/2/21
 * 简 述：<功能简述>
 */
public class MenuView extends LRelativeLayout<String, WlMenuViewBinding> {
    HashMap<String, String[]> hashMap;

    TextView[] tvArray1;
    TextView[] tvArray2;
    Dialog dialog;

    private int currentPosition;

    public MenuView(Context context) {
        super(context);
    }

    public MenuView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public int getContentId() {
        return R.layout.wl_menu_view;
    }

    @Override
    protected void initView(@Nullable AttributeSet attrs) {
        super.initView(attrs);
        initData();
        initArray();


    }

    public void initData() {
        hashMap = new HashMap<>();
        hashMap.put("音轨", WlSetting.getInstance().channelData);
        hashMap.put("尺寸", WlSetting.getInstance().screenData);
        hashMap.put("倍速", WlSetting.getInstance().speedData);
        hashMap.put("播放模式", WlSetting.getInstance().modeData);
        hashMap.put("色彩", WlSetting.getInstance().colorData);
    }

    private void initArray() {
        tvArray1 = new TextView[]{d.channelTv, d.sizeTv, d.speedTv, d.modeTv, d.colorTv};

        for (int i = 0; i < tvArray1.length; i++) {
            final int m = i;
            tvArray1[i].setOnClickListener(v -> {
                if (currentPosition != m) {
                    currentPosition = m;
                    setItemBg(v);
                    initArray2();
                }
            });
        }
        d.channelTv.setBackgroundColor(getHighLightColor());
        currentPosition = 0;
        tvArray2 = new TextView[]{d.item1, d.item2, d.item3, d.item4, d.item5};
        initArray2();
    }

    private void initArray2() {
        String[] strings = hashMap.get(tvArray1[currentPosition].getText().toString());
        int data = getData();
        final int leftIndex = currentPosition;
        for (int i = 0; i < tvArray2.length; i++) {
            final int m = i;
            if (i < strings.length) {
                tvArray2[i].setVisibility(View.VISIBLE);
                tvArray2[i].setText(strings[i]);
            } else {
                tvArray2[i].setVisibility(View.GONE);
            }
            tvArray2[i].setTextColor(data == i ? getHighLightColor() : Color.parseColor("#dddddd"));
            tvArray2[i].setOnClickListener(v -> {
                setData(m, leftIndex);
                initArray2();
            });
        }
    }

    private int getData() {
        int index = 0;
        switch (currentPosition) {
            case 0:
                index = WlSetting.getInstance().getChannelIndex();
                break;
            case 1:
                index = WlSetting.getInstance().getScreenSizeIndex();
                break;
            case 2:
                index = WlSetting.getInstance().getSpeedIndex();
                break;
            case 3:
                index = WlSetting.getInstance().getPlayModeIndex();
                break;
            case 4:
                index = WlSetting.getInstance().getPlayColorIndex();
                break;
        }
        return index;
    }

    private void setData(int index, int leftIndex) {
        switch (leftIndex) {
            case 0:
                WlSetting.getInstance().setChannelIndex(index);
                break;
            case 1:
                WlSetting.getInstance().setScreenSizeIndex(index);
                break;
            case 2:
                WlSetting.getInstance().setSpeedIndex(index);
                break;
            case 3:
                WlSetting.getInstance().setPlayModeIndex(index);
                break;
            case 4:
                WlSetting.getInstance().setPlayColorIndex(index);
                break;
        }

    }

    private int getHighLightColor() {
        return ActivityCompat.getColor(getContext(), R.color.theme);
    }


    private void setItemBg(View view) {
        for (TextView textView : tvArray1) {
            if (textView == view) {
                textView.setBackgroundColor(ActivityCompat.getColor(getContext(), R.color.theme));
            } else {
                textView.setBackgroundColor(Color.parseColor("#333333"));
            }
        }
    }

    public MenuView setDialog(Dialog dialog) {
        this.dialog = dialog;
        return this;
    }
}
